package cn.schina.dbfw.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCConnection {

	
	int ID ;
	
	Connection connection = null; 
	
	boolean    working    = false;

	
	public JDBCConnection(int ID, String driverName, String url, String user, String pwd) throws SQLException{
		
		this.ID = ID;
		initConnection(driverName, url, user, pwd);
	}
	
	public void initConnection(String driverName, String url, String user, String pwd) throws SQLException {
		
		if (connection == null){
		    try {
				Class.forName(driverName);
				connection = DriverManager.getConnection(url, user, pwd);
				connection.setAutoCommit(false);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} //catch (SQLException e) {
				//e.printStackTrace();
			//}
		    
		}

	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	public boolean isWorking() {
		return working;
	}

	public void setWorking(boolean working) {
		this.working = working;
	}
	
	public void destroy(){
		if (connection != null){
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public Connection getConnection() {
		return connection;
	}

	public int getID() {
		return ID;
	}

	public void setID(int id) {
		ID = id;
	}
	
	public void commit() throws SQLException
	{
		connection.commit();
	}
	
	public void rollback() throws SQLException
	{
		connection.rollback();
	}
}
